/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.servlet;

import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.DrbgParameters;

/**
 * <p>Project: javaweb - Check
 * <p>Powered by webrx On 2022-03-08 17:37:16
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@WebServlet("/session/check")
public class Check extends HttpServlet {
    /**
     * 检查用户登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        if(req.getParameter("code") == null || "".equals(req.getParameter("code").trim())){
            req.setAttribute("msg","请填验证码");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }else{
            String s1 = req.getSession().getAttribute("captcha").toString();
            String s2 = req.getParameter("code");
            if(s1.equalsIgnoreCase(s2)){
                String a = req.getParameter("account").trim();
                String p = req.getParameter("pwd").trim();
                if("root".equalsIgnoreCase(a) && "abc".equals(p)){
                    //登录成功
                    req.getSession().setAttribute("user",a);
                    req.getRequestDispatcher("index.jsp").forward(req,resp);
                }else{
                    req.setAttribute("msg","账号密码错误");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }
            }else{
                req.setAttribute("msg","验证码错误");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }
        }
    }

    /**
     * 验证码
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GifCaptcha c = new GifCaptcha();
        CaptchaUtil.out(c,req,resp);
        //req.getSession().setAttribute("captcha","sfsx");
    }
}
